package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.model.behavior.dtos.CollectionBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.common.constants.article.HotArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApCollectionBehaviorimpl implements ApCollectionBehaviorService {

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult collectBehavior(CollectionBehaviorDTO dto) {
        //校验参数 登录
        ApUser user = AppThreadLocalUtils.getUser();
        //判断参数,是否登录
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN, "未登录");
        }
        //根据条件查询,
        ApBehaviorEntry userIdOrEquipmentId = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        String id = userIdOrEquipmentId.getId();
        //根据实体id,文章id查询条件
        Query query = Query.query(Criteria.where("entryId").is(id).and("articleId").is(dto.getArticleId()));
        ApCollection collection = mongoTemplate.findOne(query, ApCollection.class);
        //如果为null,则创建
        ApCollection apCollection = new ApCollection();
        if (dto.getType() == 0) {
            if (collection == null) {
                apCollection.setCollectionTime(new Date());
                apCollection.setArticleId(dto.getArticleId());
                apCollection.setEntryId(id);
                apCollection.setType(dto.getType());
                mongoTemplate.save(apCollection);
            }
        } else if (dto.getType() == 1) {
            mongoTemplate.remove(query, ApCollection.class);
        }
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setType(UpdateArticleMess.UpdateArticleType.COLLECTION);
        mess.setArticleId(dto.getArticleId());
        mess.setAdd(dto.getOperation()==0?1:-1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(mess));
        log.info("收藏行为 发送消息 ==>{}",JSON.toJSONString(mess));

        return ResponseResult.okResult();
    }
}
